#include <iostream>
#include<cstdio>
#include <string>
#include <strings.h>
#include <cerrno>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

using namespace std;

int main()
{
    int cfd=socket(AF_INET,SOCK_STREAM,0);
    if(cfd==-1)
    {
        perror("socket");
        exit(-1);
    }

    sockaddr_in client_addr;
    memset(&client_addr, 0, sizeof(client_addr));
    client_addr.sin_family=AF_INET;
    client_addr.sin_port=htons(8080);
    inet_pton(AF_INET, "114.55.137.232", &client_addr.sin_addr);

    int n=connect(cfd,(sockaddr*)&client_addr,sizeof(client_addr));
    if(n==-1)
    {
        perror("connect");
        exit(-1);
    }

    while(true)
    {
        char buffer[1024]={0};
        cout<<"Please Enter# ";
        string tmp;
        getline(cin,tmp);
        n = send(cfd, tmp.c_str(), tmp.length(), 0);
        if(n == -1) 
        {
            perror("send");
            break;
        }

        n=recv(cfd,buffer,sizeof(buffer)-1,0);
        if(n>0)
        {
            buffer[n]=0;
            cout<<"server say: "<<buffer<<endl;
        }
        else if(n==0)
        {
            cout<<"server disconnect"<<endl;
            break;
        }
        else
        {
            perror("recv");
            break;
        }
        sleep(1);
    }

    close(cfd);


    return 0;
}
    
